package javashitang;

import vo.Person;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;

public class lambda2 {

    //求Integer集合的元素之和、乘积和最大值。
    public static void test1() {
        List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);
        // 求和方式1
        Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
        // 求和方式2
        Optional<Integer> sum2 = list.stream().reduce(Integer::sum);
        // 求和方式3
        Integer sum3 = list.stream().reduce(0, Integer::sum);

        // 求乘积
        Optional<Integer> product = list.stream().reduce((x, y) -> x * y);

        // 求最大值方式1
        Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y);
        // 求最大值写法2
        Integer max2 = list.stream().reduce(1, Integer::max);

        System.out.println("list求和：" + sum.get() + "," + sum2.get() + "," + sum3);
        System.out.println("list求积：" + product.get());
        System.out.println("list最大值：" + max.get() + "," + max2);
    }

    //求所有员工的工资之和和最高工资。
    public static void test2() {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

        int sum = personList.stream().map(e -> e.getSalary()).mapToInt(Integer::intValue).sum();

        Integer reduce = personList.stream().map(e -> e.getSalary()).reduce(0, (x, y) -> x + y);

        Optional<Integer> reduce1 = personList.stream().map(e -> e.getSalary()).reduce(Integer::sum);

        Integer reduce2 = personList.stream().reduce(0, (x, p) -> x += p.getSalary(), Integer::sum);

        //最高工资
        Integer reduce3 = personList.stream().reduce(0, (x, y) -> x > y.getSalary() ? x : y.getSalary(), Integer::max);
        Integer integer = personList.stream().map(e -> e.getSalary()).max(Comparator.comparing(Integer::intValue)).get();
        System.out.println(integer);

        System.out.println(reduce3);
    }

    //统计员工人数、平均工资、工资总额、最高工资。
    public static void test3() {
        List<Person> personList = new ArrayList<Person>();

        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));

        Long collect = personList.stream().collect(Collectors.counting());

        Double collect1 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));

        Optional<Integer> integer = personList.stream().
                collect(Collectors.maxBy(Comparator.comparing(Person::getSalary))).
                map(e -> e.getSalary());


        DoubleSummaryStatistics collect2 = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
        double sum = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary)).getSum();
        System.out.println(sum);


        System.out.println(integer.orElse(null));

    }

    //将员工按薪资是否高于8000分为两部分；将员工按性别和地区分组
    public static void test4() {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, "male", "New York"));
        personList.add(new Person("Jack", 7000, "male", "Washington"));
        personList.add(new Person("Lily", 7800, "female", "Washington"));
        personList.add(new Person("Anni", 8200, "female", "New York"));
        personList.add(new Person("Owen", 9500, "male", "New York"));
        personList.add(new Person("Alisa", 7900, "female", "New York"));

        //Map<Boolean, List<Person>> collect = personList.stream().collect(Collectors.partitioningBy(e -> e.getSalary() > 8000));

        //按性别分组
        //Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(e -> e.getSex()));

        //将员工先按性别分组，再按地区分组
        Map<String, Map<String, List<Person>>> collect2 = personList.stream().
                collect(Collectors.groupingBy(e -> e.getSex(), Collectors.groupingBy(e -> e.getArea())));
        System.out.println(collect2);
    }

    public static void main(String[] args) {
        test4();
    }
}
